#include "certification.h"
#include <unistd.h>
#include <shadow.h>
#include <cstring>
#include <cstdio>
#include "../com/simple_logger.h"
#include "../globe_type.h"

using namespace std;

bool Certification::Verify(const char *username, const char *password)
{
#ifdef DEBUG
    return true;
#endif
    struct spwd *sp;
    if ((sp = getspnam(username)) == NULL)
    {
        LOG4CPLUS_DEBUG(SimpleLogger::Get()->LoggerRef(), "系统未注册该用户或应用无法访问系统");
        return false;
    }
    char salt[512] = {0};
    int i, j;
    for (i = 0, j = 0; sp->sp_pwdp[i] && j != 3; i++)
    {
        if (sp->sp_pwdp[i] == '$')
        {
            j++;
        }
    }
    strncpy(salt, sp->sp_pwdp, i - 1);
    if (strcmp(sp->sp_pwdp, crypt(password, salt)) == 0)
    {
        return true;
    }
    return false;
}